(function($) {
	/**
	 * Private variable
	 */
	var ENV_FLASH_VERSION = '9.0',
	/**
	 * Client's HTML container
	 */
	container,
	/**
	 * Configure parameters
	 */
	opts;

	/**
	 * Main function
	 */
	$.fn.chatonpage = function(options) {
		try {
			container = $(this);
			opts = $.extend({}, $.fn.chatonpage.defaults, options);
			checkParameter();
			loadFlash();

			container.html(container.html() + '<br /><button>test</button>');

			$('button', this).click(function(){
				FABridge.testName.root().testAlert();
			});
			
		} catch (exception) {
			container.html(exception);
		}

		return this;
	};

	function checkParameter()
	{
		/**
		 * Check the parameters 
		 * any mistake, throw exception
		 */
		if (false) {
			throw('Error');
		}
		return true;
	}
	
	function loadFlash()
	{
		if ($.flash.available && $.flash.hasVersion(ENV_FLASH_VERSION)) {
			var flashHtml = $.flash.create({
				swf: 'Client.swf',
				width: 764,
				height: 556,
				flashvars: {bridgeName: 'testName'}
			});
		} else {
			throw('The brower should enable the flash addon, and reqiure' + 
				  'its version is greater than '+ ENV_FLASH_VERSION);
		}
		container.html(flashHtml);
	}

	/**
	 * Public variable
	 */
	$.fn.chatonpage.defaults = {
		// whether to work
		disable: false,
		// user's identity
		userid: 12,	
		// user's name, display in chat room 
		nickname: 'supperman',
		// user's icon
		personalImage: 'http://www.sohu.com/kkkk.jpg',
		// icon's width, unit px
		imageWidth : 300,
		// icon's height, unit px
		imageHeight : 400,
		// display panel type
		template: 'default'
	};

	/**
	 * Public methods
	 * Interactive function with Flex
	 */
	var bridgeMethods = {
		func1: function(param){alert(param);},	
		func2: function(){alert('bridge func2');},	
		func3: function(){alert('bridge func3');},	
		func4: function(){alert('bridge func4');}
	};

	$.each(bridgeMethods, function(i) {
		$.fn.chatonpage[i] = this;
	});
})(jQuery);
